/*
 * ml20gui - The MSN Webcam Recorder
 * This source code is under the GNU Public License (GPL).
 * Written by Ramiro Polla
 * Initial release: june 3rd, 2005
 *
 * this program records MSN Webcam Video
 */

#include "ml20gui.h"
#include <vfw.h>

extern int ID_TIMER;
#define AVI_TIME 100

DWORD WINAPI ExportThread( LPARAM lparam )
{
    hexport_t *thexport = (hexport_t*) lparam;

    while( thexport->avifile->exporting != 2
        && thexport->avifile->exporting != 3 )
        export_video_frame( thexport );

    if( thexport->avifile->exporting == 3 ) {
        // send messages, or else there's someone waiting for me
        SetDlgItemText( thexport->hwnd, IDC_STATIC_AVI, str_done_exporting );
        EnableWindow( GetDlgItem( thexport->hwnd, IDC_STOP ), FALSE );
        EnableWindow( GetDlgItem( thexport->hwnd, IDC_SAVE_AVI ), TRUE );
        KillTimer( thexport->hwnd, thexport->ID_TIMER );
    }

    export_video_end( thexport );

    return 0;
}

int export_video_start( hexport_t *hexport )
{
    avifile_t *avifile = hexport->avifile;
    decoder_t *decoder = hexport->decoder;

    avifile->si.dwRate = avifile->fps;
    AVIFileInit( );

    if( AVIFileOpen( &(avifile->pfile), avifile->fname, OF_CREATE, NULL ) != AVIERR_OK )
        return AVIFILEOPEN;

    if( AVIFileCreateStream((avifile->pfile), &(avifile->ps), &(avifile->si)) != AVIERR_OK )
        return AVIFILECREATESTREAM;

    avifile->popts[0] = &avifile->opts[0];
    memset( avifile->popts[0], 0x00, sizeof(AVICOMPRESSOPTIONS) );

    avifile->opts[0].fccType = streamtypeVIDEO;
    avifile->opts[0].dwFlags = AVICOMPRESSF_DATARATE | AVICOMPRESSF_KEYFRAMES | AVICOMPRESSF_VALID;
    avifile->opts[0].dwKeyFrameEvery = -1;
    avifile->opts[0].dwQuality = -1;
    avifile->opts[0].dwInterleaveEvery = 1;

    if( AVISaveOptions( hexport->hwnd, ICMF_CHOOSE_KEYFRAME | ICMF_CHOOSE_DATARATE, 1, &avifile->ps, avifile->popts ) != TRUE )
        return AVISAVEOPTIONS;

    if( AVIMakeCompressedStream(&(avifile->psCompressed), avifile->ps, avifile->opts, NULL) != AVIERR_OK )
        return AVIMAKECOMPRESSEDSTREAM;

    if( AVIStreamSetFormat(avifile->psCompressed, 0, &(decoder->BITM), sizeof(BITMAPINFOHEADER) ) != AVIERR_OK )
        return AVISTREAMSETFORMAT;

    avifile->firsttick = GetTickCount( );
    avifile->exp_i = 0;
    avifile->imp_i = 0;
    avifile->exporting = 1;

    decode_frame( decoder, decoder->in );

    SendDlgItemMessage( hexport->hwnd, IDC_PROGRESS, PBM_SETRANGE32, decoder->in->real_time, decoder->out->real_time );

    hexport->ID_TIMER = ID_TIMER++;
    SetTimer( hexport->hwnd, hexport->ID_TIMER, AVI_TIME, NULL );
    return 0;
}

int export_video_frame( hexport_t *hexport )
{
    avifile_t *avifile = hexport->avifile;
    decoder_t *decoder = hexport->decoder;
    vframe_t *vframe = hexport->decoder->vframe;
    int timedif;

    if( vframe == decoder->out || !(vframe->nf) ) {
        hexport->avifile->exporting = 3; //clean me
        return FALSE;
    }

    timedif = ( vframe->nf->real_time - decoder->in->real_time ) - ( avifile->exp_i * 1000 / avifile->fps );

    if( timedif <= ( 1000 / avifile->fps ) ) {
        avifile->imp_i++;
        decode_frame( decoder, vframe->nf );
    }

    AVIStreamWrite(    avifile->psCompressed, avifile->exp_i++, 1, decoder->kol, decoder->BITM.biSizeImage, 0, NULL, NULL );
    return TRUE;
}

void export_video_end( hexport_t *hexport )
{
    clean_export( hexport, CLEAN_ALL );
    hexport->avifile->exporting = 0;
}
